﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace SkillSystem
{
    public delegate Action<AnimationEvent> OnAnimationEvent(AnimationEvent eventData);

   
    public class SkillAnimationProcesser : SkillItemProcesserBase
    {
        public event OnAnimationEvent OnAnimationStart;

        public event OnAnimationEvent OnAnimationEnd;

        public ActiveSkillTransmitter CurSkillTransmitter;

        public bool IsCurAnimationFinish;

        AnimationEvent curEventData;

        Animator animator;
        public SkillAnimationProcesser(ISkillTransmitter skillTransmitter, Animator anim)
        {
            CurSkillTransmitter = (ActiveSkillTransmitter)skillTransmitter;
            animator = anim;
            InitEvent();
        }

        private void InitEvent()
        {
            CurProcesserState = SkillProcesserState.End;

            AnimationClip[] infos = animator.runtimeAnimatorController.animationClips;
            //WaitTime = infos[0].length;
            for (int i = 0; i < infos.Length; i++)
            {
                int index = -1;
                for (int j = 0; j < CurSkillTransmitter.SkillItems.Length; j++)
                {
                    if (CurSkillTransmitter.SkillItems[j].CurSkillItemData.ActionName == infos[i].name)
                    {
                        index = j;
                    }
                }

                if (index==-1)
                {
                    continue;
                }
                AnimationEvent animationEndEvent = new AnimationEvent();
                animationEndEvent.functionName = "AnimationEnd";
                animationEndEvent.time = infos[i].length;
                animationEndEvent.stringParameter = infos[i].name;
                animationEndEvent.intParameter = CurSkillTransmitter.SkillItems[index].CurSkillItemData.ID;
                animationEndEvent.floatParameter = CurSkillTransmitter.SkillID;

                infos[i].AddEvent(animationEndEvent);

                AnimationEvent animationStartEvent = new AnimationEvent();
                animationStartEvent.functionName = "AnimationStart";
                animationStartEvent.time = 0;
                animationStartEvent.stringParameter = infos[i].name;
                animationStartEvent.floatParameter = CurSkillTransmitter.SkillID;
                animationStartEvent.intParameter = CurSkillTransmitter.SkillItems[index].CurSkillItemData.ID;
                infos[i].AddEvent(animationStartEvent);
            }
        }

        public override void Do()
        {
            CurProcesserState = SkillProcesserState.Playing;
            animator.SetBool(CurSkillTransmitter.CurSkillItem.CurSkillItemData.ActionParamsName, true);
        }

        public void AnimationStart(AnimationEvent data)
        {
            int skillIndex = (int)data.floatParameter;
            int skillItemID= data.intParameter;
            if (skillIndex== CurSkillTransmitter.SkillID
                &&skillItemID == CurSkillTransmitter.CurSkillItem.CurSkillItemData.ID)
            {
                IsCurAnimationFinish = false;
                curEventData = data;
                if (OnAnimationStart != null)
                {
                    OnAnimationStart(data);
                }
            }
        }

        public void AnimationEnd(AnimationEvent data)
        {
            int skillIndex = (int)data.floatParameter;
            int skillItemID = data.intParameter;
            if (skillIndex == CurSkillTransmitter.SkillID
                && skillItemID == CurSkillTransmitter.CurSkillItem.CurSkillItemData.ID)
            { 
                IsCurAnimationFinish = true;
                CurProcesserState = SkillProcesserState.End;
                if (OnItemCompleted != null)
                {
                    OnItemCompleted();
                }
            }
        }

        public override void OnSkillCompleted()
        {
            for (int i = 0; i < CurSkillTransmitter.SkillItems.Length; i++)
            {
                animator.SetBool(CurSkillTransmitter.SkillItems[i].CurSkillItemData.ActionParamsName, false);
            }
        }

        public override void Break()
        {
            CurProcesserState = SkillProcesserState.End;
        }
    }
}
